﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.OAWeb.Base;
using System.Collections;
using System.Data;

namespace Basic4Net.OAWeb.Module_Report
{
    public partial class TeacherMeritPayByTerm : BasePage
    {
        private static MeritPay _meritpay;
        protected static MeritPay HMeritPay
        {
            get { return _meritpay ?? (_meritpay = new MeritPay()); }
        }

        protected bool bIsSuper = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            int iTeacherType = Convert.ToInt32(Request["Type"]); //获取当前页面所代表的教工身份 1-在职 2 临聘
            int teachID = int.Parse(Session["UserID"].ToString());
            bIsSuper = ExistSuper(teachID);
            if (!bIsSuper)
            {
                ddlSearchType.Visible = false;
                txtCode.Visible = false;
                btnSearch.Visible = false;
            }
            else
            {
                ddlSearchType.Visible = true;
                txtCode.Visible = true;
                btnSearch.Visible = true;
            }
            if (!IsPostBack)
            {
                
                BindSearchType();
                BindMonths(iTeacherType);
            }

            BindData(iTeacherType);
        }

        protected void BindSearchType()
        {
            ddlSearchType.Items.Add(new ListItem("按编号查询", "0"));
            ddlSearchType.Items.Add(new ListItem("按姓名查询", "1"));
        }

        protected void DrDateChanged(object sender, EventArgs e)
        {
            int iTeacherType = Convert.ToInt32(Request["Type"]); //获取当前页面所代表的教工身份 1-在职 2 临聘
            BindData(iTeacherType);
        }

        protected void BtnSearchClick(object sender, EventArgs e)
        {
            int iTeacherType = Convert.ToInt32(Request["Type"]); //获取当前页面所代表的教工身份 1-在职 2 临聘
            BindData(iTeacherType);
        }


        private void BindData(int iTeacherType)
        {
            int teachID = int.Parse(Session["UserID"].ToString());
            bool bIsSuper = ExistSuper(teachID);
            int year;
            int month;
            string[] strarr = drDate.SelectedValue.Split(new char[] { '-' });
            if (strarr.Length != 2)
                return;

            Int32.TryParse(strarr[0], out year);
            Int32.TryParse(strarr[1], out month);
            if (year < 1 || month < 1)
                return;

            string strSearch = ddlSearchType.SelectedValue;
            int iSearchType = int.Parse(strSearch);

            var list = HMeritPay.GetTeacherMeritPayList(year, month, iTeacherType, iSearchType, this.txtCode.Text.Trim(), bIsSuper,teachID);

            DataTable dt = new DataTable();
            string[] strNames = { "TeacherCode", "TeacherName", "MeritPayLevel", "AmountIssued" };
            for (int j = 0; j < strNames.Length; j++)
            {
                DataColumn dc = new DataColumn();
                dc.ColumnName = strNames[j];
                dc.DataType = typeof(object);
                dt.Columns.Add(dc);
            }
            double dSumAmountIssued = 0.0;
            foreach (var li in list)
            {
                DataRow dr = dt.NewRow();
                dr["TeacherCode"] = li["TeacherCode"];
                dr["TeacherName"] = li["TeacherName"];
                dr["MeritPayLevel"] = li["MeritPayLevel"];
                dr["AmountIssued"] = li["AmountIssued"];
                dt.Rows.Add(dr);
                dSumAmountIssued += li["AmountIssued"] == null ? 0 : Math.Round(Convert.ToDouble(li["AmountIssued"]), 2);
            }
            int PageNow = 0;
            DataView dv = dt.DefaultView;
            System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dv;
            ps.AllowPaging = true;
            ps.PageSize = 20;
            if (this.txt_Go.Text.ToString() == "")
                this.txt_Go.Text = "1";
            if (this.txt_Go.Text.ToString() == "0")
                this.txt_Go.Text = "1";
            if (Convert.ToInt32(this.txt_Go.Text.ToString()) >= ps.PageCount)
                this.txt_Go.Text = ps.PageCount.ToString();
            PageNow = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
            ps.CurrentPageIndex = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;

            pRepeater.DataSource = ps;
            pRepeater.DataBind();
            this.lbl_ToPage.Text = this.txt_Go.Text.ToString();
            this.lbl_PageCount.Text = ps.PageCount.ToString();

            dSumAmountIssued = dSumAmountIssued == null ? 0 : Math.Round(dSumAmountIssued, 2);
            if (list != null && list.Count > 0 && bIsSuper)
            {
                ScriptManager.RegisterStartupScript(pUpdatePanel, this.GetType(), "AddSumRow", "AddSumRow(" + dSumAmountIssued.ToString() + ");", true);
            }
            ScriptManager.RegisterStartupScript(pUpdatePanel, this.GetType(), "Load", "Load();", true);
        }

        /// <summary>
        /// 根据教工身份，来获取对应的绩效工资的年月，来填充年月下拉框
        /// </summary>
        /// <param name="iTeacherType"></param>
        private void BindMonths(int iTeacherType)
        {
            IList<string> yearList = HMeritPay.GetMeritPayMonth(SchoolMark, iTeacherType);
            if (yearList == null || yearList.Count < 1)
            {
                drDate.Items.Add(new ListItem("--当前没有绩效工资的历史数据--", "-1"));
                return;
            }
            drDate.DataSource = yearList;
            drDate.DataBind();
        }


//        select t.rid as TeacherID, t.teachName as TeacherName, 
//t.teachCode as TeacherName, 
//tm.AmountIssued,
//(case tm.AmountIssued
//    when 800 then '一等'
//    when 600 then '二等'
//    when 400 then '三等'
//    else '其他'
//    end
//) as Level
//from t_MeritPay tm
//right join dbo.teacherinfo t on tm.TeacherID = t.rid
//where tm.teacherid = 3
//and tm.TeachType = 1
//and tm.FYear = 2010 and tm.FMonth = 10
    }
}